/**
 * @file models/pdtdashboard.js 仪表盘
 * @author
 */
import { routerRedux } from 'dva/router';
import api from '../api';

export default {
  namespace: 'pdtdashboard',
  state: {
    productList: [],
    assetAllocation: [],
    // assetEffectiveDate: '',
    kpiEffectiveDate: '',
    bmKpis: {},
    portKpis: {},
    maxKpis: {},
    minKpis: {},
    additionalKpis: {},
    portfolioCode: '',
    series: [],
    chartTableList: [],
  },
  effects: {
    * getProductList({}, { call, put }) {
      const response = yield call(api.getPdtList);
      yield put({
        type: 'getProductListSuccess',
        response: response.data.slice(0, 20),
      });
    },
    * getKpiData({ payload: query }, { call, put }) {
      const response = yield call(api.getKpiData, query);
      yield put({
        type: 'getKpiDataSuccess',
        response: response.data,
      });
    },
    * getAllInfo({}, { call, put, select }) {
      const pdtList = yield select(state => state.pdtdashboard.productList);
      let portfolioCode;
      if (pdtList.length) {
        portfolioCode = pdtList[0].portfolioCode;
      } else {
        const response = yield call(api.getPdtList);
        yield put({
          type: 'getProductListSuccess',
          response: response.data.slice(0, 20),
        });
        portfolioCode = response.data[0].portfolioCode;
      }
      const res = yield call(api.getKpiData, { portfolioCode });
      const response = yield call(api.getPortfolioratio, { portfolioCode, systemId: 's001' });
      yield put({
        type: 'getKpiDataSuccess',
        response: res.data,
      });
      yield put({
        type: 'getPortfolioratioSuccess',
        response: response.data,
      });
    },
    * getPortfolioratio({ payload: query }, { call, put }) {
      const response = yield call(api.getPortfolioratio, query);
      yield put({
        type: 'getPortfolioratioSuccess',
        response: response.data,
      });
    },
  },
  reducers: {
    getProductListSuccess(state, action) {
      return {
        ...state,
        productList: action.response,
      };
    },
    getKpiDataSuccess(state, action) {
      const { response: { assetAllocation, assetEffectiveDate, bmKpis, kpiEffectiveDate, portKpis, additionalKpis, maxKpis, minKpis } } = action;
      return {
        ...state,
        assetAllocation,
        // assetEffectiveDate,
        kpiEffectiveDate,
        bmKpis,
        portKpis,
        maxKpis,
        minKpis,
        additionalKpis,
      };
    },
    getPortfolioratioSuccess(state, action) {
      const { response: { portfolioCode, series } } = action;
      return {
        ...state,
        portfolioCode,
        series,
      };
    },
    changePortfolioCode(state, action) {
      const { payload: { portfolioCode } } = action;
      return {
        ...state,
        portfolioCode,
      };
    },

  },
};
